//
//  ContentViewController.m
//  APP_login
//
//  Created by edz on 7/20/21.
//  Copyright © 2021 baicai. All rights reserved.
//

#import "AttentionUserVC.h"
#import "BZKTools.h"
#import "AttentionUserCell.h"
#import "AttentionUserModel.h"
#import "Masonry.h"
#import "AFNetworking.h"
#import "MJRefresh.h"
#import "JSONModel.h"

@interface AttentionUserVC () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UILabel *contentLable;
@property (nonatomic, strong) UITableView *attentionUserTabView;
@property (nonatomic, strong) NSMutableArray *modelArray;
@property (nonatomic, strong) MJRefreshNormalHeader *header;
@property (nonatomic, strong) MJRefreshBackNormalFooter *footer;
@property (nonatomic, assign) NSInteger tailID;
@property (nonatomic, assign) BOOL isLastPage;
@property (nonatomic, assign) BOOL isPost;
@end

@implementation AttentionUserVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _modelArray = [NSMutableArray array];
    _tailID = 0;
    _isLastPage = NO;
    _isPost = YES;
    _attentionUserTabView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:_attentionUserTabView];
    [_attentionUserTabView mas_makeConstraints:^(MASConstraintMaker *make){
        make.edges.mas_equalTo(self.view);
    }];
    _attentionUserTabView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _attentionUserTabView.delegate = self;
    _attentionUserTabView.dataSource = self;
    [_attentionUserTabView registerClass:[AttentionUserCell class] forCellReuseIdentifier:NSStringFromClass([AttentionUserCell class])];
    
    
    _header =  [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh:)];
    _header.lastUpdatedTimeLabel.hidden = YES;
    _attentionUserTabView.mj_header = _header;
    
    _footer =  [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh:)];
    _footer.automaticallyChangeAlpha = YES;
    _attentionUserTabView.mj_footer = _footer;
    [self getData];
}

- (void)headerRefresh:(MJRefreshNormalHeader *)header{
    _tailID = 0;
    [self getData];
}

- (void)footerRefresh:(MJRefreshBackNormalFooter*)footer{
    if (!self.isLastPage) {
        AttentionUserModel *model = self.modelArray[self.modelArray.count-1];
        self.tailID = model.attentionId;
        [self getData];
    }else{
        [self.footer endRefreshingWithNoMoreData];
    }
}

-(void)getData{
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    NSDictionary *dict = @{@"userID":@"80813", @"userKey":@"GmRlVVlIgJnCjlBIhhsYd6Oh0wEXahOc",@"tailID":@(self.tailID), @"targetUserID":@"80813"};
    if (_isPost) {
        _isPost = NO;
        [manger POST:@"http://oldapi.kr-cell.net/learning-circle/follow-user-list" parameters:dict progress:^(NSProgress * _Nonnull downloadProgress){
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject){
            self.isLastPage = [[responseObject objectForKey:@"isEnd"] boolValue];
            NSMutableArray *array= [AttentionUserModel arrayOfModelsFromDictionaries:[responseObject objectForKey:@"followUserList"] error:nil];
            if (array) {
                if (self.tailID == 0) {
                    [self.modelArray removeAllObjects];
                    self.modelArray = array;
                }else{
                    [self.modelArray addObjectsFromArray: array];
                }
                [self.attentionUserTabView reloadData];
            }
            //加载至最后一页又从头刷新
            if (!self.isLastPage && self.footer.state == MJRefreshStateNoMoreData) {
                [self.footer resetNoMoreData];
            }
            [self endRefresh];
            self.isPost = YES;
        } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error){
            NSLog(@"请求出错:%@", error);
            [self endRefresh];
            self.isPost = YES;
        }];
    }
}
- (void) endRefresh{
    if (self.header.isRefreshing) {
        [self.header endRefreshing];
    }
    if (self.footer.isRefreshing) {
        if (!self.isLastPage) {
            [self.footer endRefreshing];
        }else{
            [self.footer endRefreshingWithNoMoreData];
        }
    }
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;
}

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AttentionUserCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AttentionUserCell class])];
    if (cell == nil) {
        cell = [[AttentionUserCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:NSStringFromClass([AttentionUserCell class])];
    }
    cell.model = _modelArray[indexPath.row];
    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
